package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.ReservationDto;
import com.zzyl.entity.Reservation;
import com.zzyl.entity.TimeCount;
import com.zzyl.mapper.ReservationMapper;
import com.zzyl.service.CustomerReservationService;
import com.zzyl.vo.ReservationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CustomerReservationServiceImpl implements CustomerReservationService {
    @Autowired
    private ReservationMapper reservationMapper;

    @Override
    public void addReservation(ReservationDto reservationDto) {
        Reservation reservation=new Reservation();
        BeanUtils.copyProperties(reservationDto,reservation);
        reservationMapper.addReservation(reservation);
    }

    @Override
    public PageResponse<ReservationVo> getByPage(Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<Reservation> page=reservationMapper.getByPage(status);
        PageResponse<ReservationVo> reservationVoPageResponse=PageResponse.of(page,ReservationVo.class);
        return reservationVoPageResponse;
    }

    @Override
    public void cancel(Long id) {
        reservationMapper.cancel(id);
    }

    @Override
    public Integer getCancelledReservationCount() {
        return reservationMapper.getCancelledReservationCount();
    }

    @Override
    public List<TimeCount> getCountByTimeCount(LocalDateTime time) {
        LocalDateTime endTime=time.plusDays(1);
        return reservationMapper.getCountByTimeCount(time,endTime);
    }


    public void updateReservationStatus(LocalDateTime timeNow) {
        reservationMapper.updateReservationStatus(timeNow);
    }

}
